<?php

namespace app\admin\controller\publics;

use think\Db;

/**
 * 类型管理
 */
class Types
{

	protected static $instance;

	public static $cardType = [1 => "贷记卡", 2 => "借记卡"];

	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 获取卡类型
	 * @return array|string
	 */
	public function get_card_type($ids = "", $tp = "array")
	{
		if ($ids == "") {
			$cardTypeList = self::$cardType;
		} else {
			$cardTypeList = [];
			$idsList = explode(",", $ids);
			foreach ($idsList as $k => $v) {
				if (self::$cardType[$v]) {
					$cardTypeList[$v] = self::$cardType[$v];
				}
			}
		}
		if ($tp == 'array') {
			$list = [];
			foreach ($cardTypeList as $key => $val) {
				$list[] = ["id" => $key, "name" => $val];
			}
			return $list;
		}
		if ($tp == 'string') {
			// dump($cardTypeList);
			return join(",", $cardTypeList);
		}
	}

	/**
	 * 获取交易类型
	 * @return object|string|bool
	 */
	public function get_trans_type($tp = 'All', $str = "", $group_field = "")
	{
		if ($tp == "All" || $tp == "str") {
			if ($tp == 'All') {
				return \app\common\model\TransactionType::where("status = 1")->select();
			}
			if ($tp == 'str' && !empty($group_field)) {
				$res =  \app\common\model\TransactionType::whereIn("code", $str)->value("group_concat(" . $group_field . ")");
				if ($res) return $res;
				return "ALL_TYPE";
			}
		}
		return false;
	}

	/**
	 * 获取收款类型
	 * @return object|string|bool
	 */
	public function get_route_type($tp = 'All', $str = "", $group_field = "")
	{

		if ($tp == "All" || $tp == "str") {
			if ($tp == 'All') {
				return \app\common\model\ConsumeChannel::where(["status" => 1])->select();
			}
			if ($tp == 'str' && !empty($group_field)) {
				$res = \app\common\model\ConsumeChannel::whereIn("code", $str)->value("group_concat(" . $group_field . ")");
				if ($res) return $res;
				return "ALL_TYPE";
			}
		}
		return false;
	}
}
